﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections;

namespace WMSTest.OnlineMapRequests
{
    //Implements WMS 1.3 based on http://webhelp.esri.com/arcgisserver/9.3/Java/wms_service_in_a_browser.htm
    class WMSMapRequest : MapRequest
    {
        //Required
        private string _version; //"VERSION"
        private string _service; //"WMS" 
        private string _request;  //"GetMap"
        private string[] _layers; 
        private string[] _styles; 
        private string _crs;  //coordinate reference system
        private Extent _bbox;
        private int _width;
        private int _height;
        private string _format;  //"image/png" or "image/jpeg"

        //Optional
        public Boolean _transparent; 
        public string _bgColor;  //hex code of color 0xFFFFFF or FFFFFF

        public WMSMapRequest(string endpointUrl, string version, string[] layers, string[] styles, string crs)
            : base(endpointUrl, RequestMethod.GET)
        {
            this._version = version;
            this._service = "WMS";
            this._request = "GetMap";
            this._layers = layers;
            this._styles = styles;
            this._crs = crs;
            this._format = "image/png";

            this._transparent = false;
            this._bgColor = "";
        }

        public WMSMapRequest(string endpointUrl, string version, string service, string request,
            string[] layers, string[] styles, string crs, string format, Boolean transparent, string bgColor)
            : base(endpointUrl, RequestMethod.GET)
        {
            this._version = version;
            this._service = service;
            this._request = request;
            this._layers = layers;
            this._styles = styles;
            this._crs = crs;
            this._format = format;

            this._transparent = transparent;
            this._bgColor = bgColor;
        }

        #region IOnlineMapRequest Members

        public override HttpWebResponse PerformExportMapRequest()
        {
            //  ?VERSION=1.3.0&REQUEST=GetMap&CRS=CRS:84&BBOX=-178.217598,18.924782,-66.969271,71.406235&WIDTH=765
            //  &HEIGHT=360&LAYERS=0,1&STYLES=,&FORMAT=image/png&BGCOLOR=0xFFFFFF&TRANSPARENT=TRUE


            //setup the paramtable
            httpRequest.paramTable = new Hashtable();


            httpRequest.paramTable.Add("VERSION", _version);
            //httpRequest.paramTable.Add("SERVICE", _service);
            httpRequest.paramTable.Add("REQUEST", _request);
            httpRequest.paramTable.Add("LAYERS", String.Join(",", _layers));
            httpRequest.paramTable.Add("STYLES", String.Join(",", _styles));
            httpRequest.paramTable.Add("CRS", _crs);
            httpRequest.paramTable.Add("BBOX", _bbox.ToString());
            httpRequest.paramTable.Add("WIDTH", _width);
            httpRequest.paramTable.Add("HEIGHT", _height);
            httpRequest.paramTable.Add("FORMAT", _format);


            //httpRequest.paramTable.Add("TRANSPARENT", _transparent);
            //httpRequest.paramTable.Add("BGCOLOR", BgColor);

            //then call parent method
            return httpRequest.IssueRequest();
        }

        public override void SetBbox(double xmin, double ymin, double xmax, double ymax)
        {
            if (_bbox == null)
                _bbox = new Extent(xmin, ymin, xmax, ymax);
            else
            {
                _bbox.xmin = xmin;
                _bbox.ymin = ymin;
                _bbox.xmax = xmax;
                _bbox.ymax = ymax;
            }
        }

        public override void SetSize(int width, int height)
        {
            _width = width;
            _height = height;
        }

        #endregion

        #region Properties

        public override string ImageFormatString 
        { 
            get { return _format; }
        }

        public string Version
        {
            get { return _version;}
            set { _version = value; }
        }


        #endregion
    }
}
